﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading;

using BeeZ.CommonObjects.Interfaces;
using BeeZ.CommonObjects.Honey;

namespace BeeZ.Client
{

    namespace Bees
    {
        /// <summary>
        /// A general implementation of a bee.
        /// </summary>
        public abstract class AbstractBee : IBee
        {
            public event Collect OnGetHoney;
            protected Boolean fly = true;
            protected IHoney honey;
            protected Hashtable infoTable = new Hashtable();

            /// <param name="i">Time interval between the bees checkups. 0 if you don't want a loop</param>
            /// <param name="args">The bears you want to sign up for honey</param>
            public AbstractBee(int i, params IBear[] args)
            {
                infoTable.Add("Type", this.GetType());
                infoTable.Add("Interval", i * 1000);
                foreach (IBear b in args)
                {
                    OnGetHoney += new Collect(b.Eat);
                }
            }

            virtual public void StartFlying(object o)
            {
                Console.WriteLine("{0} started flying.", this.GetType().ToString());
                while (true)
                {

                    //Retrive information and update the infoTable
                    SuckFlower();

                    //Create a new Honey on a new thread
                    //ThreadPool.QueueUserWorkItem(new WaitCallback(CreateHoney));
                    CreateHoney(null);

                    if ((int)infoTable["Interval"] == 0)
                    {
                        break;
                    }

                    System.Threading.Thread.Sleep((int)infoTable["Interval"]);

                }
            }

            virtual public void Pause()
            {
                if (fly)
                {
                    fly = false;
                    Console.WriteLine("{0} paused.", this.GetType().ToString());
                }
            }

            virtual public void Resume()
            {
                if (!fly)
                {
                    fly = true;
                    Console.WriteLine("{0} Resumed.", this.GetType().ToString());
                }
            }

            /// <summary>
            /// Retrieves information which later will be sent as honey.
            /// </summary>
            virtual public void SuckFlower()
            {
                if (!fly)
                {
                    return;
                }
                //Console.WriteLine("{0} is sucking the flower", this.GetType().ToString());
                infoTable["Time"] = DateTime.Now;
            }

            /// <summary>
            /// Encapsulate the information retrieved in a honey object.
            /// </summary>
            virtual public void CreateHoney(object o)
            {
                if (!fly)
                {
                    return;
                }
                Console.WriteLine("{0} made some Honey.", this.GetType().ToString());
                honey = new SimpleHoney(infoTable);
                OnGetHoney(honey);
            }

        }

        public class SleepingBee : AbstractBee
        {
            /// <summary>
            /// Creats a sleeping bee. requires the Hive and the sleeping interval in seconds.
            /// </summary>
            public SleepingBee(int i, params IBear[] args)
                : base(i, args)
            {
            }

            public override void StartFlying(object o)
            {
                base.StartFlying(null);
            }

            public override void SuckFlower()
            {
                string s;
                s = "woke up after " + ((int)infoTable["Interval"] / 1000).ToString() + " secs";

                base.SuckFlower();
                infoTable["Comment"] = s;
            }

        }

    }
}